// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "postTitle": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "房源标题"
  },
  "postContent": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "房源描述"
  },
  "postSummary": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "房源摘要"
  },
  "postThumbnail": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "房源缩略图"
  },
  "postStatus": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "正在出租",
            "value": 0
          },
          {
            "text": "已经出租",
            "value": 1
          }
        ]
      }
    ],
    "defaultValue": 1,
    "label": "房源状态"
  },
  "rentStart": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "datetime"
      }
    ],
    "label": "承租日期"
  },
  "rentEnd": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "datetime"
      }
    ],
    "label": "承租结束日期"
  },
  "landlordName": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "房东姓名"
  },
  "landlordPhone": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "房东联系电话"
  },
  "price": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "月付价格"
  },
  "area": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      }
    ],
    "label": "面积"
  },
  "roomCount": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      }
    ],
    "label": "卧室数量"
  },
  "toiletCount": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "洗手间数量"
  },
  "deposit": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "押金"
  },

  "netFee": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "网费"
  },
  "waterFee": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "水费"
  },
  "fireFee": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "燃气费"
  },
  "propertyFee": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "label": "燃气费"
  },
  "remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "备注"
  }
}

const enumConverter = {
  "postStatus_valuetotext": {
    "0": "正在出租",
    "1": "已经出租"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
